package com.icss.ah.productEval.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.productEval.bean.ProductEvalBean;
import com.icss.ah.productEval.bean.ProductInfoBean;
import com.icss.ah.productEval.service.ProductEvalService;

/**
 * 商品评价
 *
 */
@Controller
@Scope("prototype")
public class ProductEvalAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	@Autowired
	private ProductEvalService productEvalService;
	private Json jsonText=new Json();
	
	private String productName;
	private String productId;
	
	/**
	 * 去商品评价列表页面
	 * @return
	 */
	public String toProductEvalListPage(){
		
		return "toProductEvalListPage";
	}
	
	/**
	 * 去商品评价详细页面
	 * @return
	 */
	public String toProductEvalDetailPage(){
		String corpId = getBaseCorpId();	//公众号id
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("corpId", corpId);
		map.put("productId", productId);
		
		try {
			ProductInfoBean productInfo=productEvalService.getProductById(map);
			getRequest().getSession().setAttribute("productInfo", productInfo);
			infolog.info("productEvalService|getEvalList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductEvalAction|异常：",e);
		}
		
		
		return "toProductEvalDetailPage";
	}
	
	/**
	 * 获取评价商品列表
	 */
	public void getProductEvalList(){
		String userCode=getUserCodeFromSession();								//当前登录人员账号
		UserInfoBean userInfo=getUserInfo(userCode);							//登录人员信息
		String shopId=userInfo.getShopId();										//绑定店铺id
		String corpId = userInfo.getCorpId();	//公众号id
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("startIndex", getStartIndexMysql());
		map.put("endIndex", getEndIndexMysql());
		map.put("shopId", shopId);
		map.put("corpId", corpId);
		map.put("productName", productName);
		
		try {
			List<Map<String, Object>> list=productEvalService.getProductEvalList(map);
			Long total=productEvalService.getProductEvalTotal(map);
			dataGridJson=new EasyuiDataGridJson();
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
			infolog.info("productEvalService|getProductEvalList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductEvalAction|异常：",e);
		}finally {
			str2Resp(JSONObject.fromObject(dataGridJson).toString());
		}
		
		
	}
	
	/**
	 * 获取评价列表
	 */
	public void getEvalList(){
		String corpId = getBaseCorpId();	//公众号id
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("startIndex", getStartIndexMysql());
		map.put("endIndex", getEndIndexMysql());
		map.put("productId", productId);
		map.put("corpId", corpId);
		
		try {
			List<ProductEvalBean> evalList=productEvalService.getEvalList(map);
			Long total=productEvalService.getEvalTotal(map);
			dataGridJson=new EasyuiDataGridJson();
			dataGridJson.setRows(evalList);
			dataGridJson.setTotal(total);
			infolog.info("productEvalService|getProductEvalList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductEvalAction|异常：",e);
		}finally {
			str2Resp(JSONObject.fromObject(dataGridJson).toString());
		}
		
	}

	public Json getJsonText() {
		return jsonText;
	}

	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}
	

}
